package server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

import message.Message;
import message.MessageParser;
import message.types.string.LoginRequest;
import network.e2e.FrameReader;
import network.e2e.FrameWriter;
import network.link.FrameInputStream;
import network.link.FrameOutputStream;
import stalker.Stalker;

public class ClientDriver {
  
  private Socket friendlySocket;

  String hostName;
  int port;
  
  public ClientDriver() throws IOException, UnknownHostException {
    this("localhost", 4444);
  }

  public ClientDriver(String hostName, int port) throws IOException {
    this.hostName = hostName;
    this.port = port;

    friendlySocket = new Socket(hostName, port);
    
    //try to login
    FrameReader reader = new FrameReader(new FrameInputStream(friendlySocket.getInputStream()));
    FrameWriter writer = new FrameWriter(new FrameOutputStream(friendlySocket.getOutputStream()));
    
    BufferedReader bufread = new BufferedReader(new InputStreamReader(System.in));
    String usrname = bufread.readLine();
    //login request
    MessageParser.send(new LoginRequest(Stalker.GUEST_STALKER, Stalker.LOGIN_MANAGER_STALKER, usrname), writer);
    
    System.out.println("Trying to login with " + usrname);
    
    Message reply = MessageParser.retrieve(reader);
    
    //read reply
    System.out.println(reply);
    System.out.println(reply.getClient());
    System.out.println(reply.getDestination());
    
/*    //write request
    MessageParser.send(new AddDataRequest(reply.getClient())*/
    
  }
  
}
